package com.lt.basic.project.admin.controller.sys;

import com.lt.basic.project.admin.aop.Log;
import com.lt.basic.project.application.config.local.ProThreadLocal;
import com.lt.basic.project.application.service.admin.sys.ISysAdminService;
import com.lt.basic.project.common.aop.NotPower;
import com.lt.basic.project.common.core.AjaxResult;
import com.lt.basic.project.common.core.PageResult;
import com.lt.basic.project.common.validator.annotation.IDMust;
import com.lt.basic.project.pojo.validate.commons.IdValidate;
import com.lt.basic.project.pojo.validate.commons.PageValidate;
import com.lt.basic.project.pojo.validate.admin.sys.SysAdminCreateValidate;
import com.lt.basic.project.pojo.validate.admin.sys.SysAdminSearchValidate;
import com.lt.basic.project.pojo.validate.admin.sys.SysAdminUpInfoValidate;
import com.lt.basic.project.pojo.validate.admin.sys.SysAdminUpdateValidate;
import com.lt.basic.project.pojo.vo.admin.sys.SysAdminDetailVo;
import com.lt.basic.project.pojo.vo.admin.sys.SysAdminListedVo;
import com.lt.basic.project.pojo.vo.admin.sys.SysAdminSelvesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 系统用户管理
 * @author LT
 */
@RestController
@RequestMapping("admin/sys/admin")
@Api(tags = "系统用户管理")
public class SysAdminController {

    @Resource
    ISysAdminService iSysAdminService;

    /**
     * 管理员列表
     * @param pageValidate
     * @param searchValidate
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value="管理员列表")
    public AjaxResult<PageResult<SysAdminListedVo>> list(@Validated PageValidate pageValidate,
                                                                @Validated SysAdminSearchValidate searchValidate) {
        PageResult<SysAdminListedVo> list = iSysAdminService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    /**
     * 管理员信息
     * @return
     */
    @NotPower
    @GetMapping("/self")
    @ApiOperation(value="管理员信息")
    public AjaxResult<SysAdminSelvesVo> self() {
        Long adminId = ProThreadLocal.getUserId();
        SysAdminSelvesVo vo = iSysAdminService.self(adminId);
        return AjaxResult.success(vo);
    }

    /**
     * 管理员详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value="管理员详情")
    public AjaxResult<SysAdminDetailVo> detail(@Validated @IDMust() @RequestParam("id") Long id) {
        SysAdminDetailVo vo = iSysAdminService.detail(id);
        return AjaxResult.success(vo);
    }

    /**
     * 管理员新增
     * @param createValidate
     * @return
     */
    @Log(title = "管理员新增")
    @PostMapping("/add")
    @ApiOperation(value="管理员新增")
    public AjaxResult<Object> add(@Validated @RequestBody SysAdminCreateValidate createValidate) {
        iSysAdminService.add(createValidate);
        return AjaxResult.success();
    }

    /**
     * 管理员编辑
     * @param updateValidate
     * @return
     */
    @Log(title = "管理员编辑")
    @PostMapping("/edit")
    @ApiOperation(value="管理员编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody SysAdminUpdateValidate updateValidate) {
        Long adminId = ProThreadLocal.getUserId();
        iSysAdminService.edit(updateValidate, adminId);
        return AjaxResult.success();
    }

    /**
     * 管理员更新
     * @param upInfoValidate
     * @return
     */
    @NotPower
    @Log(title = "管理员更新")
    @PostMapping("/upInfo")
    @ApiOperation(value="当前管理员更新")
    public AjaxResult<Object> upInfo(@Validated @RequestBody SysAdminUpInfoValidate upInfoValidate) {
        Long adminId = ProThreadLocal.getUserId();
        iSysAdminService.upInfo(upInfoValidate, adminId);
        return AjaxResult.success();
    }

    /**
     * 管理员删除
     * @param idValidate
     * @return
     */
    @Log(title = "管理员删除")
    @PostMapping("/del")
    @ApiOperation(value="管理员删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        Long adminId = ProThreadLocal.getUserId();
        iSysAdminService.del(idValidate.getId(), adminId);
        return AjaxResult.success();
    }

    /**
     * 管理员状态
     * @param idValidate
     * @return
     */
    @Log(title = "管理员状态")
    @PostMapping("/disable")
    @ApiOperation(value="管理员状态切换")
    public AjaxResult<Object> disable(@Validated @RequestBody IdValidate idValidate) {
        Long adminId = ProThreadLocal.getUserId();
        iSysAdminService.disable(idValidate.getId(), adminId);
        return AjaxResult.success();
    }

}
